from src.mxtf_api import MxtfApi
import pandas as pd
from simple_util import SUtil

api = MxtfApi()


# 读取xlsx文件
file_path = 'doc/维权平台或律师接案股票measure action real20250226.xlsx'  # 替换为你的xlsx文件路径
df = pd.read_excel(file_path)

# 去除列名中的空格
df.columns = df.columns.str.strip()

field_mapping = {
    "parent_id": "parentId",
    "measure_entity": "measureEntity",
    "measure_date": "measureDate",
    "stock_code": "stockCode",
    "xujiachenshu_date": "xujiachenshuDate",
    "gengzheng_date": "gengzhengDate",
    "qinquan_level": "qinquanLevel",
    "qinquan_description": "qinquanDescription",
    "mark": "mark",
    "source": "source",
}
# 检查 field_mapping 中的键是否都在 DataFrame 的列名中
missing_columns = set(field_mapping.keys()) - set(df.columns)
if missing_columns:
    print(f"警告: 以下列名在 DataFrame 中不存在: {missing_columns}")
# 重命名DataFrame的列
df.rename(columns=field_mapping,inplace=True)
# 将 stockCode 列转换为字符串类型，并在左边补零
df['stockCode'] = df['stockCode'].astype(str).str.zfill(6)
df['gengzhengDate'].fillna('0', inplace=True)
# 将DataFrame中的NaN值替换为空字符串
df.fillna('', inplace=True)

# 将DataFrame转换为列表字典
data_list = df[field_mapping.values()].to_dict(orient='records')

# 打印结果
# print(data_list)
count = 1
for datas in SUtil.slice_data(data_list,200):
    # print(datas)
    res = api.import_real_measure(datas)
    print(res)
    # count+=1
    # print(f"第{count}次")
    # print(datas)
print(f'finish 总共导入数据：{len(data_list)}')